<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Welcome extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->config->load('sns_openapi');
        $url = APPPATH . "libraries" . "/tqqdemo/Tencent.php";
        require_once $url;   
        $tqqconfi = $this->config->item('tqq');
        Tencent::$debug = $tqqconfi["debug"];
        OAuth::init($tqqconfi["appkey"], $tqqconfi["secret"]);
    }

    public function index() {
        $this->load->view('welcome_message');
    }

    public function tqq() {
        $tqqconfi = $this->config->item('tqq');      
        $callback = site_url("welcome/tqq_get_aouthcode");     
        $res1 = OAuth::getAuthorizeURL($callback); //生成第一次授权请求的url地址
        redirect($res1);
    }
    public function tqq_get_aouthcode() {//处理auth认证和accesstoken的请求
            $callback = site_url("welcome/tqq_get_aouthcode");
        if (isset($_GET["code"])) {
            print_r($_GET);
            print_r($_SESSION);            
            $url = OAuth::getAccessToken($_GET["code"], $callback);
            $r = Http::request($url);
            parse_str($r, $out);
            print_r($out);
            if ($out['access_token']) {
                $_SESSION['t_access_token'] = $out['access_token'];
                $_SESSION['t_refresh_token'] = $out['refresh_token'];
                $_SESSION['t_expire_in'] = $out["expires_in"];
                $_SESSION['t_code'] = $_GET["code"];
                $_SESSION['t_openid'] = $_GET["openid"];
                $_SESSION['t_openkey'] = $_GET["openkey"];
                //  print_r($_SESSION);
                //验证授权
                $r = OAuth::checkOAuthValid();
                if ($r) {
                    $redirect_url = site_url("welcome/tqq_func_call");
                    redirect($redirect_url);
                }
            }
        }
    }

    public function tqq_func_call() {//处理第一步回调结果
        $r = Tencent::api('user/info');
        print_r(json_decode($r, true));
        echo '</pre>';
        echo "<br>";
        echo "<br>";
        echo "<br>";
        echo "实验成功";
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */